//新url解析  关于处理多个问号问题
String.prototype.urlQueryString = function () {
    var index = this.lastIndexOf("\?");
    var url = this.substring(index + 1, this.length).split('&')
    len = url.length;
    this.url = {};
    for (var i = 0; i < len; i += 1) {
        var cell = url[i].split('='),
            key = cell[0],
            val = cell[1];
        this.url['' + key + ''] = val;
    }
    return this.url;
}

$(function () {
    $('title').text('海洋咨询服务质量管理平台');

//header
    $('.head').append(' <div class="mycontent clearfix"> ' +
        '<div class="head_left left"> ' +
        '<p>海洋咨询服务质量管理平台</p> ' +
        '<span>Marine Consultation Service Quality Management Platform</span> ' +
        '</div>  ' +
        '<div class="head_right"> ' +
        '<a href="javascript:;" class="man" data-toggle="modal" data-target="#myModal">登录</a>' +
        '<a href="javascript:;" data-toggle="modal" data-target="#myModal1">注册</a> ' +
        '</div></div>');


//    轮播图
    $('#slideBox').html('  <header></header> ' +
        '<div class="hd"> ' +
        '<ul> ' +
        '<li></li> ' +
        '<li></li> ' +
        // '<li></li> ' +
        '</ul> </div> ' +
        '<div class="bd"> ' +
        '<ul> ' +
        '<li><a href="" class="ban1"></a></li> ' +
        '<li> ' +
        '<a href="" class="ban2"> ' +
        '<div class="data_con"> ' +
        '<div class="data_gif data_gif1"></div> ' +
        '<div class="data_gif data_gif2"></div> ' +
        '<div class="date_num left"> ' +
        '<div id="jigou_num"></div> ' +
        '<div>认证从业机构数量</div> ' +
        '</div> ' +
        '<div class="date_num right"> ' +
        '<div id="user_num"></div> ' +
        '<div>认证从业人员数量</div> ' +
        '</div> </div> ' +
        '</a> ' +
        '</li> ' +
        // '<li><a href="" class="ban3"></a></li> ' +
        '</ul> </div>' +
        '<!-- 下面是前/后按钮代码，如果不需要删除即可 -->' +
        '<a class="prev" href="javascript:void(0)"></a> ' +
        '<a class="next" href="javascript:void(0)"></a>')

    $('header').append(' <div class="mycontent clearfix"> ' +
        '<a href="index.html">首页</a> ' +
        '<a href="imgTest.html?id=1">工作动态</a> ' +
        '<a href="imgTest.html?id=2">通知公告</a> ' +
        '<a href="imgTest.html?id=3">信息公示</a> ' +
        '<a href="imgTest.html?id=4">规章制度</a> ' +
        '<a href="person.html">备案人员</a> ' +
        '<a href="organization.html">备案机构</a> ')


    //----轮播图------------
    //数字变化  总数2秒执行
    function countUp(elem, endVal, startVal, duration, decimal) {
        var startTime = 0;
        var dec = Math.pow(10, decimal);
        var progress, value;

        function startCount(timestamp) {
            startTime = !startTime ? timestamp : startTime;
            progress = timestamp - startTime;
            value = startVal + (endVal - startVal) * (progress / duration);
            value = (value > endVal) ? endVal : value;
            value = Math.floor(value * dec) / dec;
            elem.innerHTML = value.toFixed(decimal);
            progress < duration && requestAnimationFrame(startCount)
        }

        requestAnimationFrame(startCount)
    };
    var jigou_num = document.getElementById("jigou_num");
    var user_num = document.getElementById("user_num");

    $('.hd ul li').eq(0).addClass('on');
    $('.bd ul li').eq(0).fadeIn(0);

    var samefn = function (index) {
        setTimeout(function () {
            if (index == 1) {
                countUp(jigou_num, 435, 0, 800, 0);
                countUp(user_num, 5453, 0, 800, 0);
            }
        }, 200);

        $('.hd ul li').removeClass('on');
        $('.hd ul li').eq(index).addClass('on');
        $('.bd ul li').fadeOut(500);
        $('.bd ul li').eq(index).fadeIn(500);
    }
    //    轮播
    var index = 0;
    var movefn = function () {
        index++;
        if (index >= $('.hd ul li').length) {
            index = 0;
        }
        samefn(index);
    };
    var move = setInterval(movefn, 5000);
    //小点
    $('.hd ul li').click(function () {
        var x_index = $('.hd ul li').index($(this));
        clearInterval(move);
        samefn(x_index);
        index = x_index;
    })

//    清除自己播放动画
    $('.bd').hover(function () {
        clearInterval(move);
    }, function () {
        move = setInterval(movefn, 5000);
    })

    //    左右切换
    $('.prev').click(function () {
        clearInterval(move);
        index--;
        if (index < 0) {
            index = $('.hd ul li').length - 1;
        }
        samefn(index);
    })
    $('.next').click(function () {
        clearInterval(move);
        index++;
        if (index >= $('.hd ul li').length) {
            index = 0;
        }
        samefn(index);
    })


//footer
    $('footer').append(' Copyright @2015 All rights reserved 自然资源部海洋咨询中心 版权所有</br>' +
        '地址：北京市丰台区马官营家园3号楼　邮编：100161　电话：010-68040020</br>' +
        '建议使用IE9.0以上浏览器或兼容浏览器，分辨率1280*720 </br>')


//    登录 模态框
    $('body').append('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> ' +
        // '<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button> ' +
        '<h4 class="modal-title" id="myModalLabel">账号登录 </h4> ' +
        '</div> ' +
        '<div class="modal-body login_box login_box1"> ' +
        '<!--登录--> ' +
        '<div class="content"> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-shoujihao font1"></i> ' +
        '<input type="text" class="username" id="username1" value="" placeholder="请输入手机号"/> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-mima font2"></i> ' +
        '<input type="password" style="margin-bottom: 5px" class="psd" id="psd1" value="" placeholder="请输入密码"/> ' +
        '</div> ' +
        '<button type="button" style="margin:0 0 5px 28px" class="forget_pwd">忘记密码?</button> ' +
        '<input type="submit" class="login" id="login1" value="登录"/> ' +
        '</div> </div> ' +
        '</div><!-- /.modal-content --> </div><!-- /.modal --> </div>');


    // <!--注册 模态框-->
    $('body').append('<div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> ' +
        // '<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button> ' +
        '<h4 class="modal-title" id="myModalLabel"> 账号注册</h4> ' +
        '</div> ' +
        '<div class="modal-body login_box login_box1"> ' +
        '<!--登录--> ' +
        '<div class="content zhuce-con"> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-shoujihao font1"></i> ' +
        '<input type="text" name="" class="username" id="r_mobileid" value="" placeholder="请输入手机号"/> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-yanzhengma2 font3"></i> ' +
        '<input type="text" name="" class="code" id="r_imgcodeid" value="" placeholder="验证码"/> ' +
        '<img src="/expertlib/mobilecode/getRandomCode" alt="图片验证码" id="r_imgcodeidimg"> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-yanzhengma2 font3"></i> ' +
        '<input type="text" name="" class="phone_code" id="codeid" value="" placeholder="手机验证码"/> ' +
        '<span class="Verification_code send_yan" id="r_code">发送验证码</span> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-mima font2"></i> ' +
        '<input type="password" name="" class="psd" id="r_psd" value="" placeholder="请输入密码"/> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-mima font2"></i> ' +
        '<input type="password" style="margin-bottom: 5px" class="psd" id="r_psd2" value="" placeholder="请确认密码"/> ' +
        '</div> ' +
        '<div class="input_fu c_radio clearfix">' +
        '<input type="checkbox" name="checkbox" value="从业人员"> <span>从业人员</span>' +
        '<input type="checkbox" name="checkbox" value="从业机构"> <span>从业机构</span>' +
        '</div>' +
        '<input type="submit" name="" id="r_login" value="确定"/> ' +
        '<button type="button" class="" id="back_login">已有帐号，去<span style="color: #333333">登陆</span> </button>' +
        '</div></div> ' +
        '</div><!-- /.modal-content --> </div><!-- /.modal --> </div>');

    // <!--忘记密码 模态框-->
    $('body').append('<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> ' +
        '<div class="modal-dialog"> ' +
        '<div class="modal-content"> ' +
        '<div class="modal-header"> ' +
        // '<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button> ' +
        '<h4 class="modal-title" id="myModalLabel"> 忘记密码</h4> ' +
        '</div> ' +
        '<div class="modal-body login_box login_box1"> ' +
        '<!--登录--> ' +
        '<div class="content pass-con"> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-shoujihao font1"></i> ' +
        '<input type="text" name="" class="username" id="f_mobileid" value="" placeholder="请输入手机号"/><br/> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-yanzhengma2 font3"></i> ' +
        '<input type="text" name="" class="code" id="f_imgcodeid" value="" placeholder="验证码"/> ' +
        '<img src="/expertlib/mobilecode/getRandomCode" alt="图片验证码" id="f_imgcodeidimg"> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-yanzhengma2 font3"></i> ' +
        '<input type="text" name="" class="phone_code" id="f_codeid" value="" placeholder="手机验证码"/> ' +
        '<span class="Verification_code send_yan" id="Verification_code1">发送验证码</span> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-mima font2"></i> ' +
        '<input type="password" name="" class="psd" id="f_psd" value="" placeholder="请输入新密码"/> ' +
        '</div> ' +
        '<div class="input_fu"> ' +
        '<i class="iconfont icon-mima font2"></i> ' +
        '<input type="password" name="" style="margin-bottom: 5px" class="psd" id="f_psd2" value="" placeholder="请确认新密码"/> ' +
        '</div> ' +
        '<button type="button" class="" id="back_login1">返回登录</button> ' +
        '<input type="submit" name="" id="f_login" value="确定"/> </div> </div></div> ' +
        '</div><!-- /.modal-content --> </div><!-- /.modal --> </div>');


    //注册返回
    $(document).on('click', '#back_login', function () {
        $('#myModal1').modal('hide');
        $('#myModal').modal('show');
    })
    //忘记密码
    $(document).on('click', '.forget_pwd', function () {
        $('#myModal').modal('hide');
        $('#myModal2').modal('show');
    })
    //忘记密码 返回登录
    $(document).on('click', '#back_login1', function () {
        $('#myModal2').modal('hide');
        $('#myModal').modal('show');
    })


//判断是否登录
    $.ajax({
        datatype: 'json',
        url: '/learn/a/sys/user/getuser',
        data: {},
        type: 'get',
        success: function (data) {
            //登录
            if (data.loginName != "") {
                $('.head_right').html('<a href="">' +
                    '<img src="images/exit.png"><span class="right" style="margin: 3px 0 0 3px;">退出</span>' +
                    '</a>');
                $('header').html(' <div class="mycontent clearfix"> ' +
                    '<a href="index.html">首页</a> ' +
                    '<a href="">工作动态</a> ' +
                    '<a href="">通知公告</a> ' +
                    '<a href="">信息公示</a> ' +
                    '<a href="">规章制度</a> ' +
                    '<a href="">个人信息</a> ' +
                    '<a href="">培训信息</a> ' +
                    '<a href="">评价管理</a> ');

            } else { //未登录

            }
        },
        error: function () {

        }
    })


//登录接口
    $("#login").click(function () {
        var username = $("#username").val();
        var pwd = $("#psd").val();

        //要删除
        if (username == 1 && pwd == 1) {
            window.location.href = "userinfo.html";
        }
        //要删除  ---end

        if (pwd == '' || username == '') {
            alert("用户名或密码不能为空")
        } else {
            $.ajax({
                type: "POST",
                url: "/expertlib/a/login",
                data: {
                    'username': username,
                    'password': pwd,
                    time: new Date().getTime()
                },
                success: function (data) {
                    if (data.code == 0) {

                    } else {
                        // alert(data.message);
                    }
                }
            });
        }

    })

    //弹框登录接口
    $("#login1").click(function () {
        var username = $("#username1").val();
        var pwd = $("#psd1").val();
        if (pwd == '' || username == '') {
            alert("用户名或密码不能为空")
        } else {
            $.ajax({
                type: "POST",
                url: "/expertlib/a/login",
                data: {
                    'username': username,
                    'password': pwd,
                    time: new Date().getTime()
                },
                success: function (data) {
                    if (data.code == 0) {

                    } else {
                        // alert(data.message);
                    }
                }
            });
        }

    })


    /*  注册*/
    //检测浏览器版本
    if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE", "")) < 10) {
        alert("您的浏览器版本过低，推荐使用360急速浏览器访问系统！");
    }

    //IE10以下浏览器input提示文字效果
    if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE", "")) < 10) {
        $(function () {
            $('#r_num').val("请输入手机号");
            $('#r_imgcodeid').val("验证码");
            $('#codeid').val("手机验证码");

            $('#r_psd').attr("type", "text")
            $('#r_psd').val("请输入密码");
            $('#r_psd2').attr("type", "text")
            $('#r_psd2').val("请确认密码");


            $('#r_num').click(function () {
                $(this).val("");
            })

            $('#r_imgcodeid').click(function () {
                $(this).val("");
            })
            $('#codeid').click(function () {
                $(this).val("");
            })
            $('#r_psd').click(function () {
                $(this).attr("type", "password");
                $(this).val("");
            })
            $('#r_psd2').click(function () {
                $(this).attr("type", "password");
                $(this).val("");
            })
        })
    }


    //手机验证码
    var token = '';
    $.ajax({
        type: "post",
        url: "",
        async: false,
        data: {
            time: new Date().getTime()
        },
        success: function (data) {
            token = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // alert(textStatus);
        }
    });

    // 刷新图片验证码
    $('#r_imgcodeidimg').click(function () {
        refreshImgCode();
    })
    function refreshImgCode() {
        var imgcodeurl = "/expertlib/mobilecode/getRandomCode?datatime=" + new Date().getTime();
        $("#r_imgcodeidimg").attr("src", imgcodeurl);
    }

    //发送手机验证码
    var istrue = false;
    var countdown = 60;
    $('#r_code').click(function () {
        settime(this, 1)
    })
    function settime(val, code) {
        istrue = false;
        if (code == 1) {

            if ($("#r_mobileid").val() == '') {
                alert("请输入手机号");
                return false;
            }

            if ($("#r_imgcodeid").val() == '') {
                alert("请输入图片验证码");
                return false;
            }

            $.ajax({
                type: "post",
                url: "",
                dataType: "json",
                async: false,
                data: {
                    time: new Date().getTime(),
                    mobile: $("#r_mobileid").val(),
                    imgcode: $("#r_imgcodeid").val()
                },
                success: function (data) {
                    if (data.code == '0') {
                        alert("验证码发送成功  请注意查收");
                        refreshImgCode();
                    } else {
                        alert(data.message);
                        istrue = true;
                        return false;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    istrue = true;
                    // alert(textStatus);
                }
            });

        }
        if (istrue) {
            return false;
        }

        if (countdown == 0) {
            val.removeAttribute("disabled");
            val.value = "发送验证码";
            countdown = 60;
            return false;
        } else {
            val.setAttribute("disabled", true);
            val.value = "" + countdown + "S后可重发";
            countdown--;
        }
        setTimeout(function () {
            settime(val, 2)
        }, 1000)
    }

    // 注册
    $('#r_login').click(function () {
        var phone = $("#r_mobileid").val();
        var pwd = $("#r_psd").val();
        var code = $("#codeid").val();
        var psd2 = $("#r_psd2").val();
        var radio = $("input[name='radio']:checked").val();
        if (phone == '') {
            alert("请输入手机号");
        } else if (code == '') {
            alert("请输入手机验证码");
        } else if (pwd == '') {
            alert("请输入密码");
        } else if (pwd != psd2) {
            alert("两次密码不一致");
        } else if (radio == undefined) {
            alert("请选择从业人员或从业机构");
        } else {
            $.ajax({
                type: "post",
                url: "",
                data: {
                    'token': token,
                    'mobile': phone,
                    'code': code,
                    'password': pwd,
                    'radio': radio
                },
                success: function (data) {
                    if (data.code == 0) {
                        alert("注册成功");
                        window.location.reload();
                    } else {
                        // alert(data.message);
                    }
                }
            });
        }
    })


    /*  忘记密码*/
    //检测浏览器版本
    if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE", "")) < 9) {
        alert("您的浏览器版本过低，推荐使用360急速浏览器访问系统！");
    }

    //IE10以下浏览器input提示文字效果
    if (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion.split(";")[1].replace(/[ ]/g, "").replace("MSIE", "")) < 10) {
        $(function () {
            $('#f_mobileid').val("请输入手机号");
            $('#f_imgcodeid').val("验证码");
            $('#f_codeid').val("手机验证码");

            $('#f_psd').attr("type", "text")
            $('#f_psd').val("请输入新密码");
            $('#f_psd2').attr("type", "text")
            $('#f_psd2').val("请确认新密码");

            $('#f_mobileid').click(function () {
                $(this).val("");
            })
            $('#f_imgcodeid').click(function () {
                $(this).val("");
            })
            $('#f_codeid').click(function () {
                $(this).val("");
            })
            $('#f_psd').click(function () {
                $(this).attr("type", "password");
                $(this).val("");
            })
            $('#f_psd2').click(function () {
                $(this).attr("type", "password");
                $(this).val("");
            })
        })
    }


    // 刷新图片验证码
    $('#f_imgcodeidimg').click(function () {
        f_refreshImgCode();
    })
    function f_refreshImgCode() {
        var imgcodeurl = "/expertlib/mobilecode/getRandomCode?datatime=" + new Date().getTime();
        $("#f_imgcodeidimg").attr("src", imgcodeurl);
    }

    //发送手机验证码
    var countdown = 60;
    var istrue = false;
    $('#Verification_code1').click(function () {
        settime1(this, 1);
    })
    function settime1(val, code) {
        istrue = false;
        if (code == 1) {
            if ($("#f_mobileid").val() == '') {
                alert("请输入手机号");
                return false;
            }
            if ($("#f_imgcodeid").val() == '') {
                alert("请输入图片验证码");
                return false;
            }

            $.ajax({
                type: "post",
                url: "/expertlib/mobilecode/getBackPasswordCode",
                dataType: "json",
                async: false,
                data: {
                    time: new Date().getTime(),
                    mobile: $("#f_mobileid").val(),
                    imgcode: $("#f_imgcodeid").val()
                },
                success: function (data) {
                    if (data.code == '0') {
                        alert("验证码发送成功  请注意查收");
                        refreshImgCode(); // 刷新图片验证码
                    } else {

                        alert(data.message);
                        istrue = true;
                        return false;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    // alert(textStatus);
                }
            });
        }

        if (istrue) {
            return false;
        }

        if (countdown == 0) {
            val.removeAttribute("disabled");
            val.value = "发送验证码";
            countdown = 60;
            return false;
        } else {
            val.setAttribute("disabled", true);
            val.value = "" + countdown + "S后可重发";
            countdown--;
        }
        setTimeout(function () {
            settime(val, 2)
        }, 1000)
    }

    //忘记密码
    $('#f_login').click(function () {
        var f_username = $("#f_mobileid").val();
        var f_pwd = $("#f_psd").val();
        var f_code = $("#f_codeid").val();
        var f_psd2 = $("#f_psd2").val();
        if (f_username == '') {
            alert("用户名不能为空")
        } else if (f_code == '') {
            alert("手机验证码不能为空")
        } else if (f_pwd == '') {
            alert("密码不能为空")
        } else if (f_pwd != f_psd2) {
            alert("两次密码不一致");
        } else {
            $.ajax({
                type: "post",
                url: "/expertlib/mobilecode/modifyPwdByCode",
                data: {
                    'mobile': f_username,
                    'code': f_code,
                    'password': f_pwd
                },
                success: function (data) {
                    if (data.code == 0) {
                        alert("密码重置成功");
                        window.location.href = "login.html"
                    } else {
                        // alert(data.message);
                    }
                }
            });
        }
    })


})
